package org.game.msgBuilder;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.cfg.BuildCfg;
import org.constant.smallkey.WorkLogSmallKey;
import org.game.constant.BuildingConstant;
import org.game.db.bean.Building;
import org.game.db.bean.City;
import org.game.db.bean.Player;
import org.game.db.proxy.bean.PlayerDataProxy;
import org.game.db.vo.PlayerDataVO.GashaponVO;
import org.game.module.buildingbo.BuildingBO;
import org.game.protobuf.s2c.S2CBuildingMsg;
import org.game.protobuf.s2c.S2CBuildingMsg.BuildingEvent;
import org.game.protobuf.s2c.S2CBuildingMsg.BuildingInfo;
import org.game.protobuf.s2c.S2CBuildingMsg.TriggerWorkEvent;
import org.game.protobuf.s2c.S2CBuildingMsg.WorkLog;
import org.game.protobuf.s2c.S2CShareMsg;
import org.game.protobuf.s2c.S2CShareMsg.RewardsMsg;
import org.game.util.ProtoUnit;

import com.google.common.collect.Maps;
import com.sojoys.artifact.manager.MessageManager;
import com.sojoys.artifact.tools.ToolMap;

/**
 * 城建消息封装 Created by fxf on 2017/9/6.
 */
public class BuildingMsgBuilder {
    /**
     * 领取打工奖励消息
     */
    public static S2CBuildingMsg.GetWorkRewardMsg getWorkRewardMsg(Building building,
            List<RewardsMsg> rewards) {
        S2CBuildingMsg.GetWorkRewardMsg.Builder builder =
                S2CBuildingMsg.GetWorkRewardMsg.newBuilder();
        S2CBuildingMsg.BuildingInfo buildingInfo = buildBuildingInfoMsg(building);
        builder.setBuildingInfo(buildingInfo);
        builder.addAllRewards(rewards);
        return builder.build();
    }

    /**
     * 构建建筑信息
     * 
     * @param player
     * @return
     */
    public static S2CBuildingMsg.GetBuildingInfo buildBuildingInfoMsg(List<Building> buildingList) {
        S2CBuildingMsg.GetBuildingInfo.Builder builder =
                S2CBuildingMsg.GetBuildingInfo.newBuilder();
        if (buildingList != null) {
            buildingList.forEach(building -> {
                builder.addBuidingInfos(buildBuildingInfoMsg(building));
            });
        }
        return builder.build();
    }

    /**
     * 建筑更新信息
     * 
     * @param building
     * @param event
     * @return
     */
    public static S2CBuildingMsg.UpdateBuildingInfo buildUpdateBuildingInfoMsg(Building building,
            BuildingEvent event) {
        BuildingInfo buildingInfo = buildBuildingInfoMsg(building);
        return S2CBuildingMsg.UpdateBuildingInfo.newBuilder().setBuildingInfo(buildingInfo)
                .setEvent(event).build();
    }

    /**
     * 建筑信息消息
     *
     * @param building 建筑对象
     */
    public static S2CBuildingMsg.BuildingInfo buildBuildingInfoMsg(Building building) {
        BuildCfg cfg = building.getBuildingProxy().getCfg();
        S2CBuildingMsg.BuildingInfo.Builder builder = S2CBuildingMsg.BuildingInfo.newBuilder();
        builder.setId(Long.toString(building.getId()));
        builder.setCid(cfg.getId());
        builder.setState(building.getState());

        if (building.getFinishTime() != null) {
            builder.setFinishTime(getRemainTime(building));
        } else {
            builder.setFinishTime(0);
        }
        List<Long> roleIds = building.getRoleId();
        if (roleIds != null) {
            for (Long roleId : roleIds) {
                builder.addRoleIds(Long.toString(roleId));
            }
        }

        List<Map> logs = building.getEventLogs();
        for (Map log : logs) {
            builder.addLogs(buildWorkLog(log));
        }
        return builder.build();
    }

    /**
     * 构建抓娃娃信息
     *
     * @param pdp
     */
    public static S2CBuildingMsg.GetGashaponInfo buildGashaponInfoMsg(PlayerDataProxy pdp) {
    	GashaponVO gashaponVO = pdp.getGashaponVO();
        S2CBuildingMsg.GetGashaponInfo.Builder builder = S2CBuildingMsg.GetGashaponInfo.newBuilder();
        builder.setCatchEndTime(gashaponVO.getEndTime());
        builder.setPollRefreshCdEndTime(gashaponVO.getRefreshCD());
        builder.setEggPool(gashaponVO.getPool());
        builder.setEggPoolId(gashaponVO.getPoolId());
        return builder.build();
    }

    /**
     * 构建抓娃娃响应信息
     *
     * @param pdp
     */
    public static S2CBuildingMsg.RespStartGashapon buildRespStartGashaponMsg(long endTime) {
        S2CBuildingMsg.RespStartGashapon.Builder builder =
                S2CBuildingMsg.RespStartGashapon.newBuilder();
        builder.setCatchEndTime(endTime);
        return builder.build();
    }

    /**
     * 构建抓娃娃结果验证响应信息
     */
    public static S2CBuildingMsg.RespCheckGashaponResult buildRespCheckGashaponResultMsg(
            String eggPool, List<Integer> eggIds, List<RewardsMsg> rewards) {
        S2CBuildingMsg.RespCheckGashaponResult.Builder builder =
                S2CBuildingMsg.RespCheckGashaponResult.newBuilder();
        builder.setEggPool(eggPool);
        builder.addAllEggIds(eggIds);
        builder.addAllRewards(rewards);
        return builder.build();
    }

    /**
     * 构建刷新抓娃娃蛋池响应信息
     */
    public static S2CBuildingMsg.RespRefreshGashaponPool buildRespRefreshGashaponPoolMsg(int eggPoolId,
            String eggPool, long endTime) {
        S2CBuildingMsg.RespRefreshGashaponPool.Builder builder =
                S2CBuildingMsg.RespRefreshGashaponPool.newBuilder();
        builder.setEggPoolId(eggPoolId);
        builder.setEggPool(eggPool);
        builder.setPollRefreshCdEndTime(endTime);
        return builder.build();
    }

    public static void pushWorkLog(Player player, int buildCid, Map<Integer, Object> log) {
        TriggerWorkEvent.Builder b =
                TriggerWorkEvent.newBuilder().setLog(BuildingMsgBuilder.buildWorkLog(log));
        b.setCid(buildCid);
        player.getPlayerProxy().sendMsg(
                MessageManager.me()
                        .create(BuildingBO.triggerWorkEvent, ProtoUnit.toByte(b.build())));
    }

    public static WorkLog.Builder buildWorkLog(Map log) {
        WorkLog.Builder workLog = WorkLog.newBuilder();
        // Integer cid = (Integer) log.get(WorkLogSmallKey.CID);
        int cid = ToolMap.getInt(WorkLogSmallKey.CID, log, 0);
        Integer type = (Integer) log.get(WorkLogSmallKey.TYPE);
        Integer time = (Integer) log.get(WorkLogSmallKey.TIME);
        Integer text = (Integer) log.get(WorkLogSmallKey.INDEX);
        workLog.setEventId(cid);
        workLog.setType(type);
        workLog.setLogTime(time);
        if (text != null) {
            workLog.setTextIndex(text);
        }
        Map<Integer, Integer> rewards =
                ToolMap.getMap(WorkLogSmallKey.REWARDS, log, Maps.newHashMap());
        for (Entry<Integer, Integer> e : rewards.entrySet()) {
            workLog.addReward(RewardsMsg.newBuilder().setId(e.getKey()).setNum(e.getValue()));
        }
        return workLog;
    }


    private static int getRemainTime(Building building) {
        return (int) (building.getFinishTime().getTime() / 1000);
    }

    /**
     * 获取城市信息
     */
    public static S2CBuildingMsg.CityInfo buildCityInfoMsg(S2CShareMsg.ChangeType ct, City city){
        S2CBuildingMsg.CityInfo.Builder builder = S2CBuildingMsg.CityInfo.newBuilder();
        return builder.setCt(ct)
                .setCid(city.getCid())
                .setInDating(city.isInDating())
                .build();
    }


    /**
     * 获取城市信息列表
     */
    public static S2CBuildingMsg.CityInfoList buildCityInfoList(S2CShareMsg.ChangeType ct,List<City> cityList){
        S2CBuildingMsg.CityInfoList.Builder builder = S2CBuildingMsg.CityInfoList.newBuilder();
        cityList.forEach(city->builder.addCityInfo(buildCityInfoMsg(ct,city)));
        return builder.build();
    }

}
